package com.sharemarking.system.controller;

import com.sharemarking.system.mapper.OrderMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/exhibitApplyResultExcel")
public class ReportController {

    @Autowired
    private OrderMapper om;

    /**
     * 导出页面
     *
     * @param model
     */
    @GetMapping("index")
    @PreAuthorize("hasPermission('base','all')")
    public String index(Model model) {

        return "exhibitApplyExcel/form";
    }

    /**
     * 导出页面
     *
     * @param model
     */
    @GetMapping("export")
    @PreAuthorize("hasPermission('base','all')")
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String type = request.getParameter("type");
        response.reset();
        response.setContentType("application/octet-stream; charset=utf-8");
        String title = null;
        if (type.equals("0")) {
            title = "产废单位报表";
        } else if (type.equals("1")) {
            title = "经营单位报表";
        } else if (type.equals("2")) {
            title = "运输单位报表";
        }
        byte[] b_iso88591 = title.getBytes("utf-8");
        String fileName = new String(b_iso88591, "iso-8859-1");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
        OutputStream outputStream = response.getOutputStream();
        outputStream.flush();
        List<String> titleList = new ArrayList<>();
        titleList.add("订单编号#code");
        titleList.add("最早转移时间#transfer_start");
        titleList.add("最晚转移时间#transfer_end");
        titleList.add("车辆要求#car_requirement");
        titleList.add("是否紧急#is_urgent");
        titleList.add("备注#notes");
        titleList.add("订单状态#state");
        titleList.add("运输距离#distance");
        titleList.add("运输费用#transport_fees");
        titleList.add("支付方#payer");
        titleList.add("抵达产废单位时间#arrival_time");
        titleList.add("车牌号#car_number");
        titleList.add("实际转移时间#actual_transfer_time");
        titleList.add("订单完成时间#complete_time");
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        List<Map<String, Object>> list = om.getExcelList(map);
//        XSSFWorkbook book = ExportExcel.getWorkBook(titleList, list);
//        book.write(outputStream);
//        book.close();
    }


}
